//
//  RHBrandIDVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/9/21.
//

#import "RHBrandIDVC.h"
#import "NetworkConfig.h"

@interface RHBrandIDVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong) UITableView *tableView;

@property(nonatomic,strong)NSArray *dataArray;
@end

@implementation RHBrandIDVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"Select BrandID";
    self.view.backgroundColor = CFTabViewBgColor;
   
    [self setupUI];
}
-(void)setupUI{
    
    self.dataArray = @[@"120000100066",@"120000100073",@"120000100012",@"120000100071"];
    
    QMUIButton *sureBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [sureBtn setTitle:@"Sure" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    sureBtn.frame = CGRectMake(0, 0, 50, 40);
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithCustomView:sureBtn];
    self.navigationItem.rightBarButtonItem = rightBarButton;
    [sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *tipsLab = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 200, 10)];
    tipsLab.font = [UIFont systemFontOfSize:16];
    tipsLab.text = @"Please select Country";
    [self.view addSubview:tipsLab];
    [tipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(15);
            make.top.equalTo(self.view).offset(15);
    }];
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [_tableView setBackgroundColor:UIColor.whiteColor];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [_tableView setSeparatorColor:kRGB(240, 240, 240) ];
    _tableView.estimatedRowHeight = 500;
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    _tableView.layer.cornerRadius = 8;
    _tableView.layer.masksToBounds = YES;
    [self.view addSubview:_tableView];
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    if (@available(iOS 15.0, *)) {
        _tableView.sectionHeaderTopPadding = 0;
    }
    
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.right.mas_offset(-15);
        make.top.equalTo(tipsLab.mas_bottom).offset(15);
        make.bottom.mas_offset(-(kSafe_bottom+20));
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"product"];
    if(cell==nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"product"];
    }
   
    if([self.dataArray[indexPath.row] isEqualToString:[NetworkConfig sharedConfig].brandID])
    {
        UIImageView *rightImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
        rightImg.image = [UIImage imageNamed:@"submit_sagreeuccess"];
        cell.accessoryView = rightImg;
    }else{
        cell.accessoryView = nil;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    for (UITableViewCell *cell in tableView.visibleCells) {
        cell.accessoryView = nil;
    }
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIImageView *rightImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    rightImg.image = [UIImage imageNamed:@"submit_success"];
    cell.accessoryView = rightImg;
    [NetworkConfig sharedConfig].brandID = self.dataArray[indexPath.row];
}

-(void)sureAction
{
    
    [self.navigationController popViewControllerAnimated:YES];

}


@end
